package cc.decat.escript.runtime;

import cc.decat.escript.codegen.BlockGenerate;
import cc.decat.escript.object.ESObject;

import java.util.LinkedList;

// 当前线程上下文
public class Context {

    private final GlobalContext globalContext;
    private final ESObject[] stack;
    public int top;
    private final LinkedList<CallInfo> callInfoList;
    private long callCount;
    public Closure closure;
    public Env env;

    public Context(GlobalContext globalContext) {
        this.globalContext = globalContext;
        this.stack = new ESObject[1024];
        this.callInfoList = new LinkedList<>();
        this.callCount = 0;
        this.closure = null;
    }

    public void push(ESObject obj) {
        stack[top++] = obj;
    }

    public ESObject pop() {
        return stack[--top];
    }

    public ESObject peek() {
        return stack[top - 1];
    }

    public GlobalContext getGlobalContext() {
        return globalContext;
    }

    public void pushEnv() {

    }

    public void popEnv() {
    }

    public void entryEnv(int childBlockIndex) {
        BlockGenerate blockGenerate = env.getSymbolTable().getBlockGenerate().getChildBlockGenerateList().get(childBlockIndex);
        env = new Env(env, blockGenerate.getSymbolTable());
    }

    public void leaveEnv() {
        env = env.getParent();
    }

    public Closure getClosure() {
        return closure;
    }

    public void setClosure(Closure closure) {
        this.closure = closure;
    }
}
